<template>
  <div class="app-container">
    <!-- 添加或修改生产报工记录对话框 -->
    <el-dialog :title="title" v-if="feedBackOpen" :visible.sync="feedBackOpen" :modal="false" width="1200px">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="报工类型" prop="feedbackType">
              <el-select v-model="form.feedbackType" placeholder="请选择报工类型">
                <el-option
                  v-for="dict in dict.type.mes_feedback_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="生产工单" prop="workorderCode">
              <el-input v-model="form.workorderCode" readonly="readonly"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="生产任务" prop="taskCode">
              <el-input v-model="form.taskCode" readonly="readonly"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="产品编码" prop="itemCode">
              <el-input v-model="form.itemCode" readonly="readonly"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="产品名称" prop="itemName">
              <el-input v-model="form.itemName" readonly="readonly"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单位" prop="unitOfMeasure">
              <el-input v-model="form.unitOfMeasure" readonly="readonly"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="计价方式" prop="pricingMethod">
              <el-select v-model="form.pricingMethod" readonly="readonly" placeholder="请选择来计价方式">
                <el-option
                  v-for="dict in dict.type.pro_pricing_method"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                  :disabled="true"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="form.pricingMethod=='PIECEWORK'">
            <el-form-item label="标准计件单价" prop="price">
              <el-input v-model="form.price" readonly="readonly" placeholder="请输入单价"/>
            </el-form-item>
          </el-col>
          <el-col :span="8" v-else-if="form.pricingMethod=='TIME'">
            <el-form-item label="标准计时单价" prop="price">
              <el-input v-model="form.price" readonly="readonly" placeholder="请输入单价"/>
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="form.pricingMethod=='TIME'">
            <el-form-item label="工作时长" prop="workingHours">
              <el-input-number :min="1" :step="1" v-model="form.workingHours" placeholder="请输入工作时长"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="规格型号" prop="specification">
              <el-input
                v-model="form.specification"
                type="textarea"
                readonly="readonly"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.isCheck == 'N'">
          <el-col :span="8">
            <el-form-item label="报工数量" prop="quantityFeedback">
              <el-input readonly="readonly" v-model="form.quantityFeedback"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="合格品数量" prop="quantityQualified">
              <el-input-number
                :min="0"
                @change="handleQuantityChanged"
                v-model="form.quantityQualified"
                placeholder="请输入合格品数量"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="不良品数量" prop="quantityUnquanlified">
              <el-input-number
                :min="0"
                @change="handleQuantityChanged"
                v-model="form.quantityUnquanlified"
                placeholder="请输入不良品数量"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-else>
          <el-col :span="8">
            <el-form-item label="报工数量" prop="quantityFeedback">
              <el-input readonly="readonly" v-model="form.quantityFeedback"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="待检测数量" prop="quantityUncheck">
              <el-input-number
                :min="0"
                @change="handleQuantityChanged"
                v-model="form.quantityUncheck"
                placeholder="请输入待检测数量"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="报工人" prop="nickName">
              <el-input v-model="form.nickName" readonly="readonly" placeholder="请选择报工人"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="报工时间" prop="feedbackTime">
              <el-date-picker
                clearable
                v-model="form.feedbackTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择报工时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8" v-if="form.isPra != 'N'">
            <el-form-item label="审核人" prop="recordNick">
              <el-input v-model="form.recordNick" placeholder="请选择审核人">
                <el-button
                  slot="append"
                  @click="handleUser2Select"
                  icon="el-icon-search"
                ></el-button>
              </el-input>
            </el-form-item>
            <UserSingleSelect
              ref="user2Select"
              @onSelected="onUser2Selected"
            ></UserSingleSelect>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          @click="handleSubmit"
          v-hasPermi="['mes:pro:feedback:tijiao']"
        >保存并提交
        </el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import UserSingleSelect from "@/components/userSelect/single.vue";
import { getTaskInfo } from "@/api/mes/pro/protask";
import { addFeedback } from "@/api/mes/pro/feedback";

export default {
  name: "feedBack",
  components: {
    UserSingleSelect,
  },
  dicts: ["mes_feedback_type", "pro_pricing_method"],
  data() {
    return {
      // 弹出层标题
      title: "生产报工",
      feedBackOpen: false,
      form: {
        recordId: null,
        feedbackType: null,
        workstationId: null,
        workstationCode: null,
        workstationName: null,
        routeId: null,
        routeCode: null,
        processId: null,
        processCode: null,
        processName: null,
        workorderId: null,
        workorderCode: null,
        workorderName: null,
        taskId: null,
        taskCode: null,
        taskName: null,
        itemId: null,
        itemCode: null,
        itemName: null,
        specification: null,
        quantity: 0,
        quantityUncheck: 0,
        quantityFeedback: 0,
        quantityQualified: 0,
        quantityUnquanlified: 0,
        userName: null,
        nickName: null,
        feedbackChannel: null,
        feedbackTime: new Date(),
        recordUser: null,
        recordNick: null,
        status: "APPROVING",
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        workingHours: 1,
      },
      // 表单校验
      rules: {
        feedbackType: [
          {required: true, message: "报工类型不能为空", trigger: "change"},
        ],
        taskCode: [
          {required: true, message: "请选择生产任务", trigger: "blur"},
        ],
        workorderCode: [
          {required: true, message: "生产工单不能为空", trigger: "blur"},
        ],
        nickName: [
          {required: true, message: "请选择报工人", trigger: "blur"},
        ],
        feedbackTime: [
          {required: true, message: "请选择报工时间", trigger: "blur"},
        ],
      },
    };
  },

  methods: {
    init(taskId) {
      getTaskInfo(taskId).then(response => {
        if (response.code == 200 && response.data) {
          this.taskInfo = response.data.taskInfo;
          this.workorderInfo = response.data.workorderInfo;

          this.form.taskId = this.taskInfo.taskId;
          this.form.taskCode = this.taskInfo.taskCode;
          this.form.taskName = this.taskInfo.taskName;
          this.form.workstationId = this.taskInfo.workstationId;
          this.form.workstationCode = this.taskInfo.workstationCode;
          this.form.workstationName = this.taskInfo.workstationName;
          this.form.processId = this.taskInfo.processId;
          this.form.processCode = this.taskInfo.processCode;
          this.form.processName = this.taskInfo.processName;
          this.form.routeId = this.taskInfo.routeId;
          this.form.routeCode = this.taskInfo.routeCode;
          this.form.isCheck = this.taskInfo.isCheck;
          this.form.nickName = this.taskInfo.attr1;
          this.form.userName = this.taskInfo.attr3;
          this.form.pricingMethod = this.taskInfo.pricingMethod;
          this.form.price = this.taskInfo.price;
          this.form.isPra = this.taskInfo.isPra;

          this.form.quantityFeedback = 0
          this.form.quantityUncheck = 0;
          this.form.quantityQualified = 0;
          this.form.quantityUnquanlified = 0;

          this.form.workorderId = this.workorderInfo.workorderId;
          this.form.workorderCode = this.workorderInfo.workorderCode;
          this.form.workorderName = this.workorderInfo.workorderName;
          this.form.itemId = this.workorderInfo.productId;
          this.form.itemCode = this.workorderInfo.productCode;
          this.form.itemName = this.workorderInfo.productName;
          this.form.specification = this.workorderInfo.productSpc;
          this.form.unitOfMeasure = this.workorderInfo.unitOfMeasure;
          this.feedBackOpen = true;
        }
      })
    },
    // 取消按钮
    cancel() {
      this.feedBackOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recordId: null,
        feedbackType: null,
        workstationId: null,
        workstationCode: null,
        workstationName: null,
        routeId: null,
        routeCode: null,
        processId: null,
        processCode: null,
        processName: null,
        workorderId: null,
        workorderCode: null,
        workorderName: null,
        taskId: null,
        taskCode: null,
        taskName: null,
        itemId: null,
        itemCode: null,
        itemName: null,
        specification: null,
        quantity: 0,
        quantityUncheck: 0,
        quantityFeedback: 0,
        quantityQualified: 0,
        quantityUnquanlified: 0,
        userName: null,
        nickName: null,
        feedbackChannel: null,
        feedbackTime: new Date(),
        recordUser: null,
        recordNick: null,
        status: "PREPARE",
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        workingHours: 1,
      };
      this.resetForm("form");
    },

    //点击人员选择按钮
    handleUserSelect() {
      this.$refs.userSelect.showFlag = true;
    },
    //人员选择返回
    onUserSelected(row) {
      this.form.nickName = row.nickName;
      this.form.userName = row.userName;
    },
    //点击人员选择按钮
    handleUser2Select() {
      this.$refs.user2Select.showFlag = true;
    },
    //人员选择返回
    onUser2Selected(row) {
      this.form.recordUser = row.userName;
      this.form.recordNick = row.nickName;
    },

    handleQuantityChanged() {
      this.form.quantityFeedback =
        this.form.quantityQualified +
        this.form.quantityUnquanlified +
        this.form.quantityUncheck;
    },

    handleSubmit() {
      this.$refs[ "form" ].validate((valid) => {
        if (valid) {
          this.form.status = "APPROVING";
          addFeedback(this.form).then((response) => {
            this.$modal.msgSuccess("提交成功");
            this.feedBackOpen = false;
            this.getList();
          });
        }
      });
    },
  },
};
</script>
